<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Orderdate extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
	}
	/*
	 * 返回订单日期列表
	 */
	public function index() {
    	$this->load->library ( 'pagination' );
    	// 每页显示数据条数
    	$per_page=10;//每页显示10项
    	
    	$config['base_url']=site_url('orderdate/index');
    	$this->load->model ( 'orderdate_model' );
    	
    	// 数据总数
    	$config ['total_rows'] = $this->orderdate_model->count_dates();
    	$config ['per_page'] = $per_page;
    	$config ['first_link'] = '第一页';
    	$config ['last_link'] = '最后一页';
    	$config ['next_link'] = '下一页';
    	$config ['prev_link'] = '上一页';
    	$config['uri_segment'] =3;
    	 
    	$this->pagination->initialize($config);
    	$data['links']=$this->pagination->create_links();
    	//偏移量
    	$offset=intval($this->uri->segment(3));
    	$data ['orderdates'] = $this->orderdate_model->get_dates($per_page,$offset);
    	$data ['title'] = '订单日期列表 - '.SYSTEM_NAME;
    	$this->load->view ( 'header', $data );
    	$this->load->view ( 'orderdate/index' );
    	$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到添加订单日期页面
	 */
	public function add() {
		$data ['title'] = '添加订单日期 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'orderdate/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到编辑订单日期页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'orderdate_model' );
		$data ['orderdate'] = $this->orderdate_model->load ( $id );
		$data ['title'] = '编辑订单日期 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'orderdate/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存订单日期信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$neworderdate = array (
				'date' => $this->input->post ( 'date' ),
				'type' => $this->input->post ( 'type' )
		);
		$this->load->model ( 'orderdate_model' );
		if ($id) {
			if($this->orderdate_model->update ( $id, $neworderdate )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$orderdateid=$this->orderdate_model->create ( $neworderdate );
			if(!empty($orderdateid)){
				return_json(TRUE,'','添加成功！');
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除订单日期信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$this->load->model ( 'orderdate_model' );
		if ($this->orderdate_model->delete ( $id )){
			return_json(TRUE,'','删除成功！'); 
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
}